﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfMail.Interface;
using Newtonsoft.Json;

namespace WcfMail.Common
{
   public  class NewTownJsonConvert : IConvert 
    {

        #region IConvert 成员

        /// <summary>
        /// 将字符串转换成指定的对象
        /// </summary>
        /// <typeparam name="T">对象的类型</typeparam>
        /// <param name="objectString">先前被序列化的对象字符串</param>
        /// <returns></returns>
        public T ConvertToObject<T>(string objectString) 
        {
            return JsonConvert.DeserializeObject<T>(objectString);
            
        }

        public object ConvertToObject(string objectString, Type targetType)
        {
            return JsonConvert.DeserializeObject(objectString, targetType);
        }

        /// <summary>
        /// （采用指定的序列化方式）将对象序列化成字符串
        /// </summary>
        /// <typeparam name="T">对象的类型</typeparam>
        /// <param name="targetObject">对象实例</param>
        /// <returns>序列化后的字符串</returns>
        public string ConvertToString(object  targetObject)
        {
            return JsonConvert.SerializeObject(targetObject);//, Formatting.Indented 将输出的文本格式化
        }

        //public string ConvertToString(object targetObject,Type targetType)
        //{
        //    return JsonConvert.SerializeObject(targetObject);
        //}
        #endregion
    }
}
